#!/bin/bash

# must be run with root priviledge

# 幂等追加函数到bashrc文件
# 参数1: bashrc文件路径
# 参数2: 需要追加的内容
# FIXME: 只能比较追加的内容（第二个参数）的第一行是否存在，因此：
#  A. 要么用单行内容
#  B. 如果用多行内容，则在第一行用一个独特的注释标明
append_if_not_exists() {
    local bashrc_file="$1"
    local content="$2"
    
    if ! grep -Fxq "$content" "$bashrc_file" 2>/dev/null; then
        echo "$content" >> "$bashrc_file"
        echo "✅ 已将内容追加到 $bashrc_file：$content" | head -n 1
    else
        echo "ℹ️ 内容已存在于 $bashrc_file，跳过添加：$content" | head -n 1
    fi
}


# 幂等添加 setitle 函数到 /etc/bash.bashrc
BASHRC_SETITLE='# 添加 setitle 函数（由脚本自动追加，日期：'"$(date)"'）
setitle(){
  ORIG=$PS1
  TITLE="\e]2;<$@>   \W\a"
  PS1=${ORIG}${TITLE}
}'
append_if_not_exists "/etc/bash.bashrc" "$BASHRC_SETITLE"

#
append_if_not_exists "/etc/bash.bashrc" "git config --global diff.tool meld"
append_if_not_exists "/etc/bash.bashrc" "git config --global difftool.prompt false"

# add clean lsblk alias
append_if_not_exists "/etc/bash.bashrc" "alias lsblk='lsblk | grep -v loop'"

# add clean df alias
append_if_not_exists "/etc/bash.bashrc" "alias df='df -h -x squashfs -x tmpfs -x devtmpfs'" 

# Ollama setting
append_if_not_exists "/etc/bash.bashrc" "export OLLAMA_MODELS=/data/ollama/.ollama"
append_if_not_exists "/etc/bash.bashrc" "export OLLAMA_GPU=cuda"
append_if_not_exists "/etc/bash.bashrc" "start_ollama.sh"


